//
//  BestListenViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/5.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "BestListenViewController.h"
#import "CommendBestListenTableViewCell.h"
#import "BestMusicListViewController.h"

@interface BestListenViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UITableView *bestMoreTableView;
@property (nonatomic, strong)NSMutableArray *nomalArr;
@property (nonatomic, strong)NSMutableArray *specielArr;
@property (nonatomic, assign)NSInteger pageId;
@property (nonatomic, strong)NSMutableArray *listArr;
//页码
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)BOOL isRefresh;
@end

@implementation BestListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"精品单听";
    
    [self initBestMoreTableView];
}

/**
 *  初始化mlTableView
 */
- (void)initBestMoreTableView
{
    self.dataArray = [NSArray array];
    self.bestMoreTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,64, FM_Screen_Width,FM_Screen_Height-64) style:UITableViewStylePlain];
    self.bestMoreTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.bestMoreTableView.dataSource = self;
    self.bestMoreTableView.delegate = self;
    [self.view addSubview:_bestMoreTableView];
    
    [self.bestMoreTableView registerClass:[CommendBestListenTableViewCell class] forCellReuseIdentifier:@"bestMoreCell"];
    
    // 发送节目榜单请求
    [self setupRefresh];
}


// UITableViewDataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Width*80 + FM_Screen_Pts_Height*20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat
{
    CommendBestListenTableViewCell *bestMoreCell = [tableView dequeueReusableCellWithIdentifier:@"bestMoreCell"];
    bestMoreCell.listenDic = [self.dataArray objectAtIndex:indexPat.row];
    return bestMoreCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BestMusicListViewController *bestMusicListVC = [[BestMusicListViewController alloc] init];
    NSDictionary *bestMusicListDic = [self.dataArray objectAtIndex:indexPath.row];
    bestMusicListVC.bestMusicDictionary = bestMusicListDic;
    [self.navigationController pushViewController:bestMusicListVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  网络请求
 */
- (void)getDataWithUrl
{
    if (!self.isRefresh) {
        // 加载更多
        self.page++;
    }else{
        // 刷新
        self.page=1;
    }
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?device=iPhone&page=%ld&per_page=20&scale=2", self.page];
    [NetWorkManager GET:url isCaChe:NO resultData:^(id responseObject) {
        if (responseObject != nil) {
            NSArray *tempArr = [responseObject objectForKey:@"list"];
            if (self.page == 1) {
                self.dataArray = [NSMutableArray array];
            }
            self.dataArray = [self.dataArray arrayByAddingObjectsFromArray:tempArr];
            [self.bestMoreTableView reloadData];
            [self setupEndRefresh];
        }
        // 移除加载更多
        if ([[responseObject objectForKey:@"maxPageId"] integerValue] == self.page) {
            self.bestMoreTableView.footer = nil;
        }else{
            // 尾
            self.bestMoreTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                self.isRefresh = NO;
                [self getDataWithUrl];
            }];
        }
    } resultRrror:^(NSError *error) {
        NSLog(@"精品听单更多:error,%@",error);
    }];
}

// 设置刷新
- (void)setupRefresh
{
    // 头
    self.bestMoreTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        [self getDataWithUrl];
    }];
    [self.bestMoreTableView.header beginRefreshing];
    
    // 尾
    self.bestMoreTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        [self getDataWithUrl];
    }];
}

// 结束刷新
- (void)setupEndRefresh
{
    [self.bestMoreTableView.header endRefreshing];
    [self.bestMoreTableView.footer endRefreshing];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

@end
